<?php


namespace app\api\controller;


use app\project\service\CommunalService;
use app\project\service\ProjectService;
use app\project\service\PublicService;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Task extends Controller
{

    public function onlineHelp(){
        //查询未回复的任务
        $where['status'] = ['not in',[3,7]];
        $where['pid'] = 0;
        $where['type'] = 1;
        $where['deadline'] = ["lt",date("Y-m-d")];
        $list = PublicService::ListService("project_help",$where);
        Db::name("project_help")->where($where)->update(['status'=>6]);
        echo "未回复数".count($list);
    }


    //同步数据(拟开工时间、拟投产时间与推进计划保持一致，以后不再手动录入)
    public function syncApi(){
        $common = new Common();
        $where['type_id'] = 343;
        $where['time'] = ['neq',"已完成"];
        $list = PublicService::ListService("project_pro_advance_plan",$where);
        foreach ($list as $itme){
            unset($where);
            $proWhere['id'] = $itme['pro_id'];
            $proWhere['pid'] = $itme['pro_id'];
            //更新字段
            Db::name("project_pro")->whereOr($proWhere)->update(['plan_onstream_time'=>$itme["time"]]);
            //实际开工时间
            Db::name("project_pro")->whereOr($proWhere)->update(['onstream_time'=>$itme["time"]]);
        }

        $where['type_id'] = 350;
        $where['time'] = ['neq',"已完成"];
        $list = PublicService::ListService("project_pro_advance_plan",$where);
        foreach ($list as $itme){
            unset($where);
            $proWhere['id'] = $itme['pro_id'];
            $proWhere['pid'] = $itme['pro_id'];
            //更新字段
            Db::name("project_pro")->whereOr($proWhere)->update(['planned_production_time'=>$itme["time"]]);
        }
//        ['in',[343,350]];
    }



    //进展汇报验证(黄旗红旗更新)
    static function advancePlan()
    {
        $common = new Common();
        //获取未超过当前时间
        $time = date("Y-m-d");

        $where["time"] = ['egt', $time];
        $list = $common->public_select("project_pro_advance_plan", $where)->select();
        if ($list) {
            foreach ($list as $item) {
                if (!empty($item['end_time']) & $item['time']!="已完成") {
                    $common->handle("project_pro_advance_plan", ["id" => $item["id"],  "color" => 0]);
                }
            }
        }

        //获取超过当前时间（黄旗）
        $where['status'] = ['in', [0, 3]];
//        $time = date("Y-m-d", strtotime("-1 month"));
        $where["time"] = ['lt', $time];
        $list = $common->public_select("project_pro_advance_plan", $where)->select();
        if ($list) {
            foreach ($list as $item) {
                $common->handle("project_pro_advance_plan", ["id" => $item["id"], "color" => 2]);
            }
        }

        //获取超过1个月的数据（红旗） ['in',[0,3]];
        $time = date("Y-m-d", strtotime("-2 month"));
        $where["time"] = ['elt', $time];
        $list = $common->public_select("project_pro_advance_plan", $where)->select();
        if ($list) {
            foreach ($list as $item) {
                $common->handle("project_pro_advance_plan", ["id" => $item["id"],  "color" => 3]);
            }
        }
        return true;
    }



    //更新重复数据
    public function heavy(){
        $oldTime = strtotime("-1 month");
        $year = date("Y",$oldTime);
        $month = date("m",$oldTime);  //上月
        $where['declare_status'] = ['neq',1];
        $where['is_del'] = 0;
        $where['year'] = $year;
        $list = Db::name("project_pro_advance")->where($where)->select();

        $ids = [];
        foreach ($list as $k=>$v){
            unset($where);
            $where['type_id'] = $v['type_id'];
            $where['month'] = $v['month'];
            $where['pro_id'] = $v['pro_id'];
            $where['declare_status'] = 1;
            $count = Db::name("project_pro_advance")->where($where)->count("*");
            if($count>0){
                $ids[] = $v['id'];
            }
        }
        unset($where);
        $where['id'] = ['in',$ids];
        Db::name("project_pro_advance")->where($where)->update(['is_del'=>1]);
        echo "更新数据".count($ids);
    }

    //办结期限前三天提醒
    public function dayRemind(){
        $where['status'] = ['lt',3];
        $where['pid'] = 0;
        $time = date("Y-m-d", (strtotime("-3 day")));
        $where['finish_time'] = $time;
        $list = Db::name("project_help")->where($where)->select();

        $count = 0;
        foreach ($list as $k=>$v){
            $sumbit_id = $v['sumbit_id'];
            unset($where);
            $where['id'] = $sumbit_id;
            $name = Db::name("project_account")->where($where)->value("name");

            //提醒提交人
            unset($smsData);
            $smsData['username'] = empty($name)?"":$name;
            $smsData['time'] = $v['finish_time'];
            $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
            $code = "待定";
            $accountWhere['id'] = $sumbit_id;
            $accountPhone = Db::name("project_account")->where($accountWhere)->field("phone")->select();
//            if(!empty($accountPhone)){
//                foreach ($accountPhone as $item){
//                    $this->publicSms($item['phone'],$code,$smsData);
//                }
//            }
            //提醒提交人end

            //提醒协助人
            $accountId = array_unique(array_filter(explode(",",$v['accountId'])));
            unset($accountWhere);
            $accountWhere['id'] = ['in',$accountId];
            $accountPhone = Db::name("project_account")->where($accountWhere)->field("phone")->select();
//            if(!empty($accountPhone)){
//                foreach ($accountPhone as $item){
//                    $this->publicSms($item['phone'],$code,$smsData);
//                }
//            }
            //提醒协助人end

            $count++;
        }
        echo "发送短信".$count;

    }

    //困难直通车超过办结时间
    public function overtimeFinish(){
        $where['status'] = 2;
        $where['pid'] = 0;
        $where['finish_time'] = ['neq',''];
        $list = Db::name("project_help")->where($where)->select();

        $nowtime = date("Y-m-d");
        $count = 0;
        foreach ($list as $k=>$v){
            if($v['finish_time']<$nowtime){
                $sumbit_id = $v['sumbit_id'];
                unset($where);
                $where['id'] = $sumbit_id;
                $name = Db::name("project_account")->where($where)->value("name");
                //提醒特定用户 4 刘传钢  130  陈华
                $smsData['username'] = $name;
                $smsData['time'] = $v['finish_time'];
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $code = "SMS_240361255";
                $accountWhere['id'] = ['in',[4,130]];
                $accountPhone = Db::name("project_account")->where($accountWhere)->field("phone")->select();
                if(!empty($accountPhone)){
                    foreach ($accountPhone as $item){
                        $this->publicSms($item['phone'],$code,$smsData);
                    }

                }
                $count++;
            }

        }
        echo "发送短信".$count;

    }


    //超过48小时无人回复
    public function overtimeHelp(){
        $where['status'] = 1;
        $where['pid'] = 0;
        $addtime = strtotime('-4 day');
        $where['addtime'] = ['egt',$addtime];
        $list = Db::name("project_help")->where($where)->select();
        $nowtime = time();
        $count = 0;
        foreach ($list as $k=>$v){
            $sumbit_id = $v['sumbit_id'];
            unset($where);
            $where['id'] = $sumbit_id;
            $name = Db::name("project_account")->where($where)->value("name");

            $addtime = $v['addtime'];
            $date=floor(($nowtime-$addtime)/86400);
            $hour = 0;
            if(!empty($date)){
                $hour += $date*24;
            }
            $hour += floor(($nowtime-$addtime)%86400/3600);

            if($hour==48){
                //提醒特定用户  188 邹鑫
                $smsData['username'] = $name;
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $code = "SMS_240376069";
                $accountWhere['id'] = 188;
                $accountPhone = Db::name("project_account")->where($accountWhere)->value("phone");
                if(!empty($accountPhone)){
                    $this->publicSms($accountPhone,$code,$smsData);
                }
            }
            if($hour==72){
                //提醒特定用户  188 邹鑫
                $smsData['username'] = $name;
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $code = "SMS_240395997";
                $accountWhere['id'] = ['in',[4,130]];
                $accountPhone = Db::name("project_account")->where($accountWhere)->field("phone")->select();
                if(!empty($accountPhone)){
                    foreach ($accountPhone as $item){
                        $this->publicSms($item['phone'],$code,$smsData);
                    }

                }
            }

            $count++;

        }
        echo "发送短信".$count;
    }

    //2022年3月29日11:13:03
    //用户提交问题协助人24小时未回复
    public function helpTask(){
       //查询未回复的任务
        $where['status'] = ['not in',[3,7]];
        $where['pid'] = 0;
        $where['type'] = 0;
        $list = PublicService::ListService("project_help",$where);
        $count = 0;
        $nowtime = time();


        $time = date("Y-m-d");
        foreach ($list as $k=>$v){
            //查询最近一次回复时间
            unset($where);
            $where['pid'] = $v['id'];
            $reply = PublicService::detailService("project_help",$where,1);
            $addtime = empty($reply['addtime'])?$v['addtime']:$reply['addtime'];

            $date=floor(($nowtime-$addtime)/86400);
            $hour = 0;
            if(!empty($date)){
                $hour += $date*24;
            }
            $hour += floor(($nowtime-$addtime)%86400/3600);


            $overdue = 0;
            if(!empty($v['finish_time'])){
                if($time>$v['finish_time']){
                    $overdue = 1;
                }
            }

            if($hour>=24 | $overdue==1){
                //更改改任务状态
                unset($update);
                $update['id'] = $v['id'];
                $update['status'] = 6;
                PublicService::saveService("project_help",$update);

                $accountWhere['id'] = $v['sumbit_id'];
                $accountDetail = PublicService::detailService("project_account",$accountWhere);


                $proWhere['id'] = $v['pro_id'];
                $proDetail = Db::name("project_pro")->where($proWhere)->find();
                $title = mb_substr($proDetail['title'],0,14,'utf-8');
                $smsData['name'] = "【{$title}…】";
                $smsData['username'] = $accountDetail['name'];
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $code = "SMS_227248609";


                $accountId = array_unique(array_filter(explode(",",$v['accountId'])));
                foreach ($accountId as $item){
                    $account = Db::name("project_account")->where(['id'=>$item])->find();
                    if(!empty($account['phone'])){
                        //短信通知用户
                        $black = $this->publicSms($account['mobile'],$code,$smsData);
                    }
                }
                $count++;
            }
            $list[$k]['hour'] = $hour;
        }



        echo "未回复数".$count;
    }


    //走访进度导出
    public function daochu(){
        $where['a.type'] = 1;
        $list = Db::name("project_pro_figure")->alias("a")
            ->join("project_pro b","a.pro_id = b.id")
            ->join("project_type c","a.problem_id = c.id")
            ->where($where)
            ->field("a.visitor,a.show_time,a.content,b.condition_val,c.name,a.problem_content")->select();
        echo Db::name("project_pro_figure")->getLastSql();die;
        dump($list);die;
    }


    //点评未回复提醒用户
    public function commentTask(){
        $common = new Common();
        $startTime = date("Y-m-d H:i:s",strtotime("-4 day"));
        $where['addtime'] = ['gt',$startTime];
        $where['pid'] = 0;
        $where['type'] = 0;
        $where['reply'] = 0;
        $list = Db::name("project_pro_comment")->where($where)->field("id,pro_id,title,addtime,content,add_id")->select();
        $count = 0;
        $nowtime = time();
        foreach ($list as $k=>$v){
            $date=floor(($nowtime-strtotime($v['addtime']))/86400);
            $hour = 0;
            if(!empty($date)){
                $hour += $date*24;
            }
            $hour += floor(($nowtime-strtotime($v['addtime']))%86400/3600);
            $list[$k]['hour'] = $hour;
            //查询项目
            $proWhere['id'] = $v['pro_id'];
            $proDetail = Db::name("project_pro")->where($proWhere)->find();
            if($hour==48){
                //提醒提交人
                $code = 'SMS_232914377';
                $title = mb_substr($proDetail['title'],0,14,'utf-8');
                $smsData['name'] = "【{$title}…】";
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $account = Db::name("project_account")->where(['id'=>$v['add_id']])->find();
                if(!empty($account['phone'])){
                    $this->publicSms($account['phone'],$code,$smsData);
                }
                //提醒提交人end

                //回复给督办单位  周健  王健  邹鑫
                $supervise = [191,182,188];
                $superviseWhere['id'] = ['in',$supervise];
                $superviseList = Db::name("project_account")->where($superviseWhere)->select();
                $superviseList = array_filter(array_column($superviseList,"phone"));

                $smsData1['name'] = "【{$account['name']}】";
                $smsData1['name2'] = "【{$title}…】";
                $smsData1 = json_encode($smsData1,JSON_UNESCAPED_UNICODE);
                foreach ($superviseList as $item){
                    $this->publicSms($item,"SMS_232919478",$smsData1);
                }
                //回复给督办单位end
             }

            $count++;
        }
        echo "发送数据".$count;
    }



    //未联系企业
    public function relation(){
        $count = 0;
        if(date("d")==20){
            $month = date("Y-m");
            $where['a.library'] = 6;
            $where['b.addtime'] = ['like', "%{$month}%"];

            //本月存在走访进度的企业
            $list = Db::name("project_pro")->alias('a')
                ->join("project_pro_figure b","a.id=b.pro_id")
                ->where($where)->field("a.id,a.title")->group("a.id")->select();
            $being = array_column($list,'id');

            unset($where);
            $where['library'] = 6;
            $where['id'] = ['not in',$being];
            $list = Db::name("project_pro")->where($where)->field("id,title,duty_person")->select();

            $code = 'SMS_232894480';
            foreach ($list as $k=>$v){
                $title = mb_substr($v['title'],0,14,'utf-8');
                $smsData['name'] = "【{$title}…】";
                $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
                $accountWhere['name'] = ['in',array_filter(explode(";",$v['duty_person']))];
                $accountList = Db::name("project_account")->field("id,name,phone")->where($accountWhere)->select();
                $accountList = array_unique(array_column($accountList,"phone"));
                foreach ($accountList as $item){
                    $this->publicSms($item,$code,$smsData);
                }
                $count++;
            }
        }
        echo "发送短信".$count;
    }


    //招商过论证会25日
    public function adoptPlan(){
        $newTime = date("Y-m-d");
        //查询25天前的数据
        $endtime = date('Y-m-d', strtotime('-25 day'));
        $where['library'] = 1;
        $where['cardMeeting'] = $endtime;
        $list = Db::name("project_pro")->field("id,title,duty_person,cardMeeting")->where($where)->select();
        $code = 'SMS_232909458';
        $count = 0;
        foreach ($list as $k=>$v){
            $title = mb_substr($v['title'],0,14,'utf-8');
            $smsData['name'] = "【{$title}…】";
            $smsData = json_encode($smsData,JSON_UNESCAPED_UNICODE);
            $accountWhere['name'] = ['in',array_filter(explode(";",$v['duty_person']))];
            $accountList = Db::name("project_account")->field("id,name,phone")->where($accountWhere)->select();
            $accountList = array_unique(array_column($accountList,"phone"));
            foreach ($accountList as $item){
                $this->publicSms($item,$code,$smsData);
            }
            $count++;
        }
        echo "发送短信".$count;
    }



    //7天未登录
    public function notLogin(){
        $time = date('Y-m-d', strtotime('-7 day'));
        $where['last_time'] = ["lt",$time];
        $list = Db::name("project_account")->where($where)->select();
        $count = 0;

        foreach ($list as $k=>$v){
            $phone = $v['phone'];
            if(!empty($phone)){
                $date = $this->publicSms($phone,"SMS_232909457",'');
                $count++;
            }
        }
        echo "发送短信".$count;
    }

    //发送短信
    public function publicSms($phone,$code,$smsData){
        $sina = "湖北东土太一智慧科技";
        $black = sms($phone, $sina, $code, $smsData);
        return $black;
    }

    //月末通知（最后五天通知）
    public function endMonth(){
        $CommunalService = new CommunalService();
        $endMonth = date('Y-m-t', time());
        $endTime = bcsub(strtotime($endMonth),time());
        $endTime = bcdiv($endTime,24*60*60); //间隔天数
        $endTime = $endTime+1;
        if($endTime==5){
            $year = date("Y");
            $month = date("m");
            $where['year'] = $year;
            $where['month'] = $month;
            $where['declare_status'] = 0;
            $list = Db::name("project_pro_advance")->where($where)->select();

            $count = 0;
            foreach ($list as $key=>$val){
                //查询项目
                $proWhere['id'] = $val['pro_id'];
                $proDetail = ProjectService::DetailService($proWhere);
                $advanceWhere['id'] = $val['type_id'];
                $advanceDetail = $CommunalService->advanceDetail($advanceWhere);
                $leader = explode(";",$proDetail['leader']); //责任人
                $duty_person = explode(";",$proDetail['duty_person']);
                foreach ($duty_person as $k => $v) {
                    $accountWhere['name'] = $v;
                    $account = ProjectService::AccountService($accountWhere);
                    if (!empty($account['phone'])) {
                        $smsData3 = [];
                        $smsData3['com'] = "【".$v."】";
                        $smsData3['name'] = "【".$proDetail['title']."】";
                        $smsData3['time'] = $month;
                        $smsData3['title'] = "【".$advanceDetail['name']."】";
                        $smsData3 = json_encode($smsData3,JSON_UNESCAPED_UNICODE);
                        $phone = $account['phone'];
                        $this->publicSms($phone,"SMS_228695599",$smsData3);
                    }
                }
                $count++;
            }
            echo "发送短信".$count;

        }


    }


    //


    //逾期通知
    public function beOverdue(){
        $CommunalService = new CommunalService();
        //当前年
        $oldTime = strtotime("-1 month");
        $year = date("Y",$oldTime);
        $month = date("m",$oldTime);

        $where['year'] = $year;
        $where['month'] = $month;
        $where['declare_status'] = 0;
        $list = Db::name("project_pro_advance")->where($where)->select();


        $count = 0;
        foreach ($list as $key=>$val){
            //查询项目
            $proWhere['id'] = $val['pro_id'];
            $proDetail = ProjectService::DetailService($proWhere);
            $advanceWhere['id'] = $val['type_id'];
            $advanceDetail = $CommunalService->advanceDetail($advanceWhere);
            $leader = explode(";",$proDetail['leader']);
            $duty_person = explode(";",$proDetail['duty_person']);


            unset($accountId);
            //通知督办部门
            switch ($proDetail['library']){
                case 2:  //预备库
                    $accountId = 190;  //任炜炜
                    break;
                case 3:  //开工库
                    $accountId = 189;  //雷辉
                    break;
                case 4:  //重点库
                    $accountId = 201;  //杨智麟
                    break;
            }

            if(!empty($accountId)){
                $overseerWhere['id'] = $accountId;
                $overseer = ProjectService::AccountService($overseerWhere);
                if (!empty($overseer['phone'])) {
                    $overseerData = [];
                    $overseerData['com'] = "【".$proDetail['duty_unit']."】";
                    $overseerData['name'] = "【".$proDetail['title']."】";
                    $overseerData['time'] = $month;
                    $overseerData['title'] = "【".$advanceDetail['name']."】";
                    $overseerData = json_encode($overseerData,JSON_UNESCAPED_UNICODE);
                    $phone = $overseer['phone'];

                    $this->publicSms($phone,"SMS_234035752",$overseerData);
                }
            }


            foreach ($leader as $k => $v) {
                $accountWhere['name'] = $v;
                $account = ProjectService::AccountService($accountWhere);
                if (!empty($account['phone'])) {
                    $smsData2 = [];
                    $smsData2['name'] = "【".$proDetail['title']."】";
                    $smsData2['time'] = $month;
                    $smsData2['title'] = "【".$advanceDetail['name']."】";
                    $smsData2 = json_encode($smsData2,JSON_UNESCAPED_UNICODE);
                    $phone = $account['phone'];
                    $this->publicSms($phone,"SMS_228695582",$smsData2);
                }
            }

            foreach ($duty_person as $k => $v) {
                $accountWhere['name'] = $v;
                $account = ProjectService::AccountService($accountWhere);
                if (!empty($account['phone'])) {
                    $smsData3 = [];
                    $smsData3['com'] = "【".$v."】";
                    $smsData3['name'] = "【".$proDetail['title']."】";
                    $smsData3['time'] = $month;
                    $smsData3['title'] = "【".$advanceDetail['name']."】";
                    $smsData3 = json_encode($smsData3,JSON_UNESCAPED_UNICODE);
                    $phone = $account['phone'];
                    $this->publicSms($phone,"SMS_228845042",$smsData3);
                }
            }
            $count++;
        }
        echo "发送短信".$count;
    }


    //逾期项目判断
    public function getSet(){
        $where['library'] = 2;
        $where['start_status'] = 1;
        $where['plan_onstream_time'] = ['neq',''];
        $list = Db::name("project_pro")->where($where)->field("id,title,plan_onstream_time")->order("id desc")->select();
        $nowTime = date("Y-m-d");
        $common = new Common();
        $yuqi = 0;
        foreach ($list as $k=>$v){
            $v['plan_onstream_time'] = str_replace(".","-",$v['plan_onstream_time']);
            $v['plan_onstream_time'] = str_replace("/","-",$v['plan_onstream_time']);
            $days = $this->diffBetweenTwoDays($nowTime,$v['plan_onstream_time']);
            $list[$k]['days'] = $days;
            if($days>0){
                $UpdateComment['start_status'] = 3;
                $common->public_select("project_pro",['id'=>$v['id']])->update($UpdateComment);
                $yuqi++;
            }
        }
        echo "逾期项目".$yuqi;
    }


    //招商签约提醒
    public function signProject(){
        $where['library'] = 1;
        $where['signTime'] = ['neq',''];
        $list = Db::name("project_pro")->where($where)->field("id,title,signTime,earlyWarning")->order("id desc")->select();
        $nowTime = date("Y-m-d");
        $common = new Common();
        $yujing = 0;
        $baojing = 0;
        foreach ($list as $k=>$v){
            if(!empty($v['signTime'])){
                $v['signTime'] = str_replace(".","-",$v['signTime']);
                $list[$k]['days'] = $days = $this->diffBetweenTwoDays($nowTime,$v['signTime']);
                $earlyWarning = 0;
                if($days>0 && $v['earlyWarning']==0){
                    $earlyWarning = 1;
                    $addComment['title'] = "签约预警";
                    $addComment['content'] = $v['title']."项目已经签约逾期，请重点关注";
                    $remark = "您的项目已经超过计划签约时间";
                    $yujing++;
                }
                if($days>61 && $v['earlyWarning']!=2){
                    $earlyWarning = 2;
                    $addComment['title'] = "签约报警";
                    $remark = "您的项目已经超过计划签约时间2个月";
                    $addComment['content'] = $v['title']."项目已经超过计划签约时间2个月，请重点关注";
                    $baojing++;
                }
                //更新项目状态
                if(!empty($earlyWarning)){
                    //消息提醒
                    $addComment['pro_id'] = $v['id'];
                    $addComment['addtime'] = date("Y-m-d H:i:s");
                    $addComment['add_id'] =  -1;
                    $addComment['type'] = 2;
                    $common->handle("project_pro_comment",$addComment);
                    $UpdateComment['sign'] = 3;
                    $UpdateComment['earlyWarning'] = $earlyWarning;
                    $common->public_select("project_pro",['id'=>$v['id']])->update($UpdateComment);
                    $notice = new Notice();
                    $notice->signMessage($addComment,$remark);
                }
            }
        }
        echo "预警项目".$yujing."报警项目".$baojing;
    }

    //计算相差天数
    function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);
//        if ($second1 < $second2) {
//            $tmp = $second2;
//            $second2 = $second1;
//            $second1 = $tmp;
//        }
        return ($second1 - $second2) / 86400;
    }


    //更改项目逾期状态
    public function projectApi(){
        $where['library'] = 4;
        $where['id'] = ['gt',0];
        $list = Db::name("project_pro")->where($where)->field("id")->select();
        $lastMonth = date('m', strtotime('-1 month'));  //上月
        $planWhere['year'] = date("Y");
        $planWhere['month'] = $lastMonth;
        foreach ($list as $k=>$v){
            $planWhere['pro_id'] = $v['id'];
            $declare = Db::name("project_pro_plandetail")->where($planWhere)->value("declare");
            $list[$k]['declare'] = empty($declare)?'':$declare;


            if($declare==null){
                $project_status = 1;
            }else{
                $project_status = 0;
            }
            //更改项目逾期状态
            Db::name("project_pro")->where(['id'=>$v['id']])->update(['project_status'=>$project_status]);
        }

    }



    //任务（循环下发当天的票）
    public function bill()
    {
        $nowtime = date("Y-m-d");

        $list = Db::name("tickets_list")->where(['effective_time' => ['egt', $nowtime], 'status' => 0])->field("signcode,name,mobile,IDcard,face,effective_time,end_time")->select();

        //$list = Db::name("tickets_list")->where(['id'=>524])->field("signcode,name,mobile,IDcard,face,effective_time,end_time")->select();

        $error = 0;
        $isok = 0;
        $commonDevice = new Test();
        $token = $commonDevice->token();
        foreach ($list as $k => $v) {
            $personsCreate['employeeNo'] = $v['signcode'];
            if (!empty($v['IDcard'])) {
                $cardNos = [$v['IDcard'], $v['signcode']]; //$order['IDcard']
            } else {
                $cardNos = [$v['signcode']];
            }
            $personsCreate['personName'] = empty($v['name']) ? "欢迎光临" : $v['name'];

            $startTime = date("Y-m-d\TH:i:sP", strtotime($v['effective_time']));
            $endtTime = date("Y-m-d\T23:59:59P", strtotime($v['end_time']));
            $personsCreate['validBeginTime'] = $startTime;
            $personsCreate['validEndTime'] = $endtTime;

////            $personFace = $commonDevice->personFace($personsCreate['employeeNo'],$token);
            $cardValue = $commonDevice->cardAdd($personsCreate, $cardNos);

            //更新人脸
            if (!empty($v['face']) & $v['face'] != 'null') {
                $personsCreate1['verifyImage'] = false;
                $personsCreate1['faceImageBase64'] = $v['face'];
                $personsCreate1['employeeNo'] = $personsCreate['employeeNo'];
                $personsCreate1['personName'] = empty($v['name']) ? "欢迎光临" : $v['name'];
                $personsCreate1['validBeginTime'] = $startTime;
                $personsCreate1['validEndTime'] = $endtTime;
                $cardValue = $commonDevice->personsUpdate($personsCreate1);
            }

            if ($cardValue !== true) {
                $error++;
            } else {
                $isok++;
            }
        }
        $black = "推送失败" . $error . "张,推送成功" . $isok . "张";
        return $black;
    }

    /**
     * 批量删除过期人员
     * @author zhixuedong
     * @date 2021/2/19/019
     */
    public function batchDel()
    {
        $commonDevice = new Test();
        //获取token
        $token = $commonDevice->token();
        $list = $commonDevice->getEmployeeNos();
        $list = $list['data']['rows'];
        $nowtime = time();
        $isok = 0;
        foreach ($list as $item) {
            $employeeNo = $item['employeeNo'];
            $black = $commonDevice->personFace($employeeNo, $token);
            if ($black['code'] == 200) {
                $detail = $black['data'];
                $validEndTime = strtotime($detail['validEndTime']);
                if ($nowtime > $validEndTime) {
                    $isok++;
                    Db::name("tickets_list")->where(['signcode' => $employeeNo])->update(['status' => 2]);
                    //删除人员
                    $commonDevice->personsDelete($employeeNo, $token);
                    //删除卡号
                    $commonDevice->deletePersonCards([$employeeNo], $employeeNo, $token);
                    //权限组人员解绑
                    $commonDevice->removePersons([$employeeNo], $token);
                }
            }
        }
        echo "操作成功,删除人数" . $isok;
    }

    /**
     * 拉取设备事件
     * @throws Exception
     * @throws \think\exception\PDOException
     * @author zhixuedong
     * @date 2021/2/25/025
     */
    public function deviceList(){
        try {
            $commonDevice = new Test();
            $token = $commonDevice->token();
            $startTime = date("Y-m-d\TH:i:sP",time());
            $endtTime = date("Y-m-d\TH:i:sP",strtotime('-2minute',time()));
            $startTime = urlencode($startTime);
            $endtTime = urlencode($endtTime);
//        $major = [1,6,7,8,9,21,22,75,76,1024];
            $major = [1,75];
            $devicesList = $commonDevice->devicesList();

            $majordesc = ['1'=>'刷卡开锁','75'=>'人脸开锁','21'=>'门锁打开'];

            $i = 0;

            foreach ($devicesList['data']['rows'] as $key=>$val){
                $i++;
                foreach ($major as $k=>$v){
                    $url = "https://api2.hik-cloud.com/api/v1/open/accessControl/accessEvents/actions/list?pageNo=1&pageSize=100&deviceSerial=".$val['deviceSerial']."&major=5&minor=".$v."&startTime=".$startTime."&endTime=".$endtTime;
                    $html = '';
                    $html = $commonDevice->curl_file_get_contents($url,$token['access_token']);
                    $html = json_decode($html,true);

//                echo $val['deviceSerial'];
//                dump($html);

                    if($html['code']==200){
                        foreach ($html['data']['rows'] as $k1=>$v1){
                            $log['date_time'] = $v1['time'];
                            $log['employee_no'] = $v1['employeeNo'];
                            $log['card_no'] = $v1['cardNo'];
                            $log['device_serial'] = $val['deviceSerial'];
                            $log['device_name'] = $val['deviceSerial'];
                            $log['type'] = $v;
                            $log['description'] = $majordesc[$v];
                            $log['code'] = date("Ymd").$v1['serialNo'];

                            $id = Db::name("tickets_device_log")->where(['code'=>$log['code']])->value("id");
                            if(empty($id)){
                                Db::name("tickets_device_log")->insert($log);
                                //检票次数减一
                                $tickets['signcode'] = $v1['employeeNo'];
                                $degree = Db::name("tickets_list")->where($tickets)->value("degree");
                                if($degree>0){
                                    $update = [];
                                    $update['degree'] = $degree-1;
                                    $update['check_time'] = time();
                                    if($degree==0){
                                        $update['status'] = 2;
                                        $commonDevice->cartDel($v1['employeeNo']);
                                    }
                                    Db::name("tickets_list")->where($tickets)->update($update);
                                }
                            }
                        }
                    }else{
                        break;
                    }
//                    if($i==2){die;}
                }
            }
            echo "操作成功";
        }catch (Exception $e){
            echo "日志中断";
        }

    }


}